package com.wcj.common;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;


public class PackageGeneration {
	
	private String rootPackageName;
	private String beanName;
	private String srcPath;
	
	private String filePath;
	
	private String[] names = { "bean","dao", "dao/impl" , "service" , "service/impl" };
	
	public PackageGeneration( String rootPackageName,String beanName,String srcPath ){
		this.rootPackageName=rootPackageName;
		this.beanName= beanName;
		this.srcPath=srcPath;
	}
	
	public void write(){
		
		this.srcPath = this.srcPath.replaceAll( "\\\\", "/" );
		
		if( !this.srcPath.endsWith( "/" ) ){
			this.srcPath += "/"; 
		}
		
		this.filePath = this.srcPath+ this.rootPackageName.replaceAll( "\\.", "/" )+ "/" ;

		generationDirectory();
		
		generationService();
		generationDao();
		generationDaoImpl();
		generationServiceImpl();
	}
	
	/**
	 * 生成目录
	 */
	private void generationDirectory( ){
		for( String name : this.names ){
			String path = this.filePath + name;
			File file = new File( path );
			if( !file.exists() ){
				file.mkdirs();
			}
		}
	}
	
	/**
	 * 生成service
	 */
	private void generationService( ){
		
		String path = PackageGeneration.class.getResource( "/com/wcj/common/service.txt" ).getPath();
		
		File serviceFile = new File( path );
		try {
			String msg = FileUtils.readFileToString( serviceFile, "utf-8" );
			msg=msg.replace( "$packageName", this.rootPackageName+".service" );
			msg=msg.replace( "$beanPackageName", this.rootPackageName+".bean."+this.beanName+"Bean" );
			msg=msg.replace( "$serviceInterfaceName", this.beanName+"Service" );
			msg=msg.replace( "$beanName", this.beanName+"Bean" );
					
			File x = new File( this.filePath + "/service/"+this.beanName+"Service.java" );
			FileUtils.write( x, msg , "utf-8" );
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * 生成dao
	 */
	private void generationDao( ){
	
		String path = PackageGeneration.class.getResource( "/com/wcj/common/dao.txt" ).getPath();
		
		File serviceFile = new File( path );
		try {
			String msg = FileUtils.readFileToString( serviceFile, "utf-8" );
			msg=msg.replace( "$packageName", this.rootPackageName+".dao" );
			
			msg=msg.replace( "$beanPackageName", this.rootPackageName+".bean."+this.beanName+"Bean" );
			
			msg=msg.replace( "$daoInterfaceName", this.beanName+"Dao" );
			msg=msg.replace( "$beanName", this.beanName+"Bean" );
					
			File x = new File( this.filePath + "/dao/"+this.beanName+"Dao.java" );
			FileUtils.write( x, msg , "utf-8" );
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * 生成daoImpl
	 */
	private void generationDaoImpl( ){
		String path = PackageGeneration.class.getResource( "/com/wcj/common/daoImpl.txt" ).getPath();
		File serviceFile = new File( path );
		try {
			String msg = FileUtils.readFileToString( serviceFile, "utf-8" );
			msg=msg.replace( "$packageName", this.rootPackageName+".dao.impl" );
			
			msg=msg.replace( "$beanPackageName", this.rootPackageName+".bean."+this.beanName+"Bean" );
			
			msg=msg.replace( "$daoInterfaceName", this.beanName+"Dao" );
			
			msg=msg.replace( "$daoInterfacePackageName", this.rootPackageName+".dao."+this.beanName +"Dao" );
			
			msg=msg.replace( "$daoName", this.beanName.substring(0,1).toLowerCase() + this.beanName.substring(1,this.beanName.length() ) + "Dao" );
			
			msg=msg.replace( "$beanName", this.beanName+"Bean" );
			
			msg=msg.replace( "$daoImplClassName", this.beanName+"DaoImpl" );
			
			
			File x = new File( this.filePath + "/dao/impl/"+this.beanName+"DaoImpl.java" );
			FileUtils.write( x, msg , "utf-8" );
			
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	
	/**
	 * 生成service
	 */
	private void generationServiceImpl(){
		String path = PackageGeneration.class.getResource( "/com/wcj/common/serviceImpl.txt" ).getPath();	
		File serviceFile = new File( path );
		try {
			String msg = FileUtils.readFileToString( serviceFile, "utf-8" );
			msg=msg.replace( "$packageName", this.rootPackageName+".service.impl" );			
			msg=msg.replace( "$beanPackageName", this.rootPackageName+".bean."+this.beanName+"Bean" );
					
			msg=msg.replace( "$daoInterfacePackageName", this.rootPackageName+".dao."+this.beanName+"Dao" );
					
			msg=msg.replace( "$serviceInterfacePackageName", this.rootPackageName+".service."+this.beanName+"Service" );
				
			msg=msg.replace( "$serviceName", this.beanName.substring(0,1).toLowerCase() + this.beanName.substring(1,this.beanName.length() ) + "Service" );
			
			msg=msg.replace( "$serviceImplClassName", this.beanName+"ServiceImpl" );
			
			msg=msg.replace( "$serviceInterfaceName", this.beanName+"Service" );
			
			msg=msg.replace( "$beanName", this.beanName+"Bean" );
			
			msg=msg.replace( "$daoInterfaceName", this.beanName+"Dao" );
			
			msg=msg.replace( "$daoName", this.beanName.substring(0,1).toLowerCase() + this.beanName.substring(1,this.beanName.length() ) + "Dao" );
			
			File x = new File( this.filePath + "/service/impl/"+this.beanName+"ServiceImpl.java" );
			FileUtils.write( x, msg , "utf-8" );
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
